package com.xzy.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * Bean生命周期之实例化前
 *
 * @author xzy.xiao
 * @date 2022/9/20  10:44
 */
@Component
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {

    Logger logger = LoggerFactory.getLogger(MyInstantiationAwareBeanPostProcessor.class);

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        logger.info("实例化前 => beanClass={},beanName={}", beanClass, beanName);
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        logger.info("实例化后 => bean={},beanName={}", bean, beanName);

        /*
         * Bean创建生命周期节点：
         *
         *                                  ↓
         * [开始]-->[实例化前]-->[实例化]-->[实例化后]-->[依赖注入]-->[初始化前]-->[初始化]-->[初始化后]-->Bean
         *
         *
         * 此方法如果false，那么将跳过后面的流程，入参中的bean就是最终的Bean：
         *
         * [开始]-->[实例化前]-->[实例化]-->[实例化后]-->[依赖注入]-->[初始化前]-->[初始化]-->[初始化后]-->Bean
         *                                  ↓                                                    ↑
         *                                  ------------------------------------------------------
         */

        return true;
    }
}

